package com.weakteam.orderonline.dataresource.domain;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import javax.persistence.*;

/**
 * @author Nail
 */

@Entity
@javax.persistence.Table(name = "\"MENU\"")
public class Menu implements Serializable {

    private static final Logger log = LoggerFactory.getLogger(Menu.class);

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "\"MENU_id_seq\"")
    @SequenceGenerator(name = "\"MENU_id_seq\"", sequenceName = "\"MENU_id_seq\"")
    @Column(name = "\"id\"")
    private Integer id;

    @Column(name = "\"parent\"")
    private Integer parent;

    @Column(name = "\"order\"", nullable = false)
    private Integer order;

    @Column(name = "\"name\"", nullable = false)
    private String name;

    @Column(name = "\"description\"")
    private String description;

    @Column(name = "\"ico\"")
    private String ico;

    @Column(name = "\"image\"")
    private String image;

    @Column(name = "\"enabled\"")
    private Boolean enabled;

    @Column(name = "\"properties\"")
    private String properties;

    public String getProperties() {
        return properties;
    }

    public void setProperties(String properties) {
        this.properties = properties;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getParent() {
        return parent;
    }

    public void setParent(Integer parent) {
        this.parent = parent;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIco() {
        return ico;
    }

    public void setIco(String ico) {
        this.ico = ico;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }


    public JSONObject getJSONObject() {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("id", id);
        jsonObj.put("parent", parent);
        jsonObj.put("order", order);
        jsonObj.put("name", name);
        jsonObj.put("description", description);
        jsonObj.put("ico", ico);
        jsonObj.put("image", image);
        jsonObj.put("enabled", enabled);
        jsonObj.put("properties", properties);
        return jsonObj;
    }

    public String getJSON() {
        StringBuilder json = new StringBuilder();
        json.append("{\"id\":").append(id).append(",");
        json.append("\"parent\":").append(parent).append(",");
        json.append("\"order\":").append(order).append(",");
        json.append("\"name\":\"").append(name).append("\",");
        json.append("\"description\":\"").append(description).append("\",");
        json.append("\"ico\":\"").append(ico).append("\",");
        json.append("\"image\":\"").append(image).append("\",");
        json.append("\"enabled\":").append(enabled).append(",");
        json.append("\"properties\":\"").append(properties).append("\"}");
        return json.toString();
    }

    public void setJSON(String json) {
        if (json!=null && json.length() > 0){
            try {
                JSONParser parser = new JSONParser();
                JSONObject jObj = (JSONObject)parser.parse(json);
                this.setJSON(jObj);
            } catch (ParseException e) {
                log.error("try to parse json" + e);
            }
        }
    }

    public void setJSON(JSONObject jObj) {
        Object obj = null;

        obj = jObj.get("id");
        if (obj != null) {
            this.id = ((Long)obj).intValue();
        }

        obj = jObj.get("parent");
        if (obj != null) {
            this.parent = ((Long)obj).intValue();
        }

        obj = jObj.get("order");
        if (obj != null) {
            this.order = ((Long)obj).intValue();
        }

        obj = jObj.get("name");
        if (obj != null) {
            this.name = (String)obj;
        }

        obj = jObj.get("description");
        if (obj != null) {
            this.description = (String)obj;
        }

        obj = jObj.get("ico");
        if (obj != null) {
            this.ico = (String)obj;
        }

        obj = jObj.get("image");
        if (obj != null) {
            this.image = (String)obj;
        }

        obj = jObj.get("enabled");
        if (obj != null) {
            this.enabled = (Boolean)obj;
        }

        obj = jObj.get("properties");
        if (obj != null) {
            this.properties = (String)obj;
        }
    }

}
